package user

import (
	"github.com/gin-gonic/gin"
	user2 "server/app/api/request/message"
	"server/base"
	group2 "server/common/dao/group"
	message2 "server/common/models/message"
	"server/common/utils"
	"server/pkg/nsq"
	"server/pkg/response"
	"time"
)

var Message = message{}

type message struct{}

func (message) SendToUser(c *gin.Context) {
	var req user2.MessageSendToUserReq
	err := c.ShouldBind(&req)
	if err != nil {
		response.Error(c, req.GetErr(err))
		return
	}

	// 获取用户id
	//roleId := utils.GetUserId(c)

	// 查询好友是否存在
}

func (message) SendToGroup(c *gin.Context) {
	var req user2.MessageSendToGroupReq
	err := c.ShouldBind(&req)
	if err != nil {
		response.Error(c, req.GetErr(err))
		return
	}

	// 获取用户id
	roleId := utils.GetUserId(c)

	// 查询绑定的数组
	g, err := group2.GroupDao.GetBindGroupByUserId(roleId)
	if err != nil || g.Id == 0 {
		response.Error(c, "未绑定的组")
		return
	}

	msg := message2.GroupMessage{
		From: roleId, GroupId: g.Id, Type: req.Type, Content: req.Content, Status: "unread",
		CreateTime: time.Now().Unix(), UpdateTime: time.Now().Unix()}

	if base.NsqClient.PushMessageToNsq(nsq.NsqMessage{Topic: nsq.TOPIC_GROUP_MESSAGE, Message: msg.ToByte()}) != nil {
		response.Error(c, "发送失败")
		return
	}

	response.Success(c, gin.H{})
}
